//
//  NetworkTools + status.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/2/27.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit

extension NetworkTools{

    func loadNewLoad(cz_since since_id:Int64 = 0,max_id:Int64 = 0,finished:(result:[String:AnyObject]?)->()){
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        //since_id max_id
        //利用Token判断用户是否过期
        guard let access_token = UserAccount.shardUserAccount.access_token  else{
        
           return
        }
        
        var  paras:[String:AnyObject] = ["access_token":access_token]
        
        
        // 在Swift 中，会默认将Int64转换为NSNumber类型， 但字典不行
        if since_id > 0 {
           paras["since_id"] = NSNumber(longLong: since_id)
        }
        
        if max_id > 0{
        
            paras["max_id"] = NSNumber(longLong: max_id - 1)
        }
        
        
        request(.GET, URLString: urlString, parameters: paras, finished: finished)
    
    }

    //发布微博
    func saveText(text:String,image:UIImage?,finished:(result:[String:AnyObject]?)->()){
        //利用Token判断用户是否过期
        guard let access_token = UserAccount.shardUserAccount.access_token  else{
            
            return
        }
        
        let parameters = ["access_token":access_token,"status":text]
        if image == nil{
            let urlString = "https://api.weibo.com/2/statuses/update.json"
            
            request(.POST, URLString: urlString, parameters:parameters, finished:finished)
        }else{
            let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
            //将图像转换为二进制数据
           let  data = UIImagePNGRepresentation(image!)
           
            upload(urlString, para: parameters, data: data!, name:"pic", finished: finished)
        }
    
    }
   
}